package mblog.web.controller.desk.user;

import mblog.base.utils.MobileUtils;
import mblog.core.data.AccountProfile;
import mblog.core.data.Addresses;
import mblog.core.data.User;
import mblog.core.persist.service.AddressesService;
import mblog.core.persist.service.UserService;
import mblog.web.controller.BaseController;
import mblog.web.controller.desk.Views;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Controller
@RequestMapping("/user/profile")
public class UserProfileWxController extends BaseController{

    @Autowired
    private UserService userService;

    @Autowired
    private AddressesService addressesService;

    public void initAddress(ModelMap model,String province,String city,String location){
        List<Addresses> dataList = addressesService.findByParentId("1");

        List<Addresses> twoList = new ArrayList<>();
        if(StringUtils.isNotEmpty(province)){
            twoList = addressesService.findByParentId(province);
        }else{
            twoList = addressesService.findByParentId(String.valueOf(dataList.get(0).getId()));
        }

        List<Addresses> threeList = new ArrayList<>();
        if(StringUtils.isNotEmpty(province)){
            threeList = addressesService.findByParentId(city);
        }else{
            threeList = addressesService.findByParentId(String.valueOf(twoList.get(0).getId()));
        }

        model.put("cityList", dataList);
        model.put("twoList", twoList);
        model.put("threeList", threeList);
    }


    @RequestMapping(value = "/nick", method = RequestMethod.GET)
    public String nick(ModelMap model, HttpServletRequest request) {

        return view(Views.USER_PROFILE_NICK);
    }

    @RequestMapping(value = "/nick", method = RequestMethod.POST)
    @ResponseBody
    public String nick_post(String newNick) {
        AccountProfile profile = getSubject().getProfile();
        User view = userService.get(profile.getId());
        view.setName(newNick);

        userService.update(view);
        profile.setName(newNick);
        return "修改成功";
    }

    @RequestMapping(value = "/mobile", method = RequestMethod.GET)
    public String mobile(ModelMap model, HttpServletRequest request) {
        AccountProfile profile = getSubject().getProfile();
        User view = userService.get(profile.getId());
        model.put("view", view);
        return view(Views.USER_PROFILE_MOBILE);
    }

    @RequestMapping(value = "/mobile", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> mobile_post(String newMobile) {
        Map<String,String> map = new HashMap<>();
        map.put("stats","200");
        map.put("messages","修改成功");

        String result = MobileUtils.isPhone(newMobile);
        if(org.apache.commons.lang.StringUtils.isNotEmpty(result)){
            map.put("stats","500");
            map.put("messages",result);
            return map;
        }

        AccountProfile profile = getSubject().getProfile();
        User view = userService.get(profile.getId());

        view.setMobile(newMobile);

        userService.update(view);
        profile.setMobile(newMobile);
        return map;//"修改成功";
    }


    @RequestMapping(value = "/address", method = RequestMethod.GET)
    public String address(ModelMap model, HttpServletRequest request) {

        AccountProfile profile = getSubject().getProfile();
        User view = userService.get(profile.getId());

        initAddress(model,view.getProvince(),view.getCity(),view.getLocation());

        model.put("view", view);
        return view(Views.USER_PROFILE_ADDRESS);
    }


    @RequestMapping(value = "/address", method = RequestMethod.POST)
    @ResponseBody
    public String address_post(String oprovince,String ocity,String olocation) {
        AccountProfile profile = getSubject().getProfile();
        User view = userService.get(profile.getId());

        view.setProvince(oprovince);
        view.setCity(ocity);
        view.setLocation(olocation);

        userService.update(view);

        profile.setProvince(oprovince);
        profile.setCity(ocity);
        profile.setLocation(olocation);

        return "修改成功";
    }


}
